package pl.edu.agh;

import java.net.URL;
import java.util.Set;

import pl.edu.agh.xenapi.Connection;
import pl.edu.agh.xenapi.Console;
import pl.edu.agh.xenapi.PIF;
import pl.edu.agh.xenapi.Session;
import pl.edu.agh.xenapi.VBD;
import pl.edu.agh.xenapi.VIF;
import pl.edu.agh.xenapi.VM;


public class SimpleTest {
	
    protected static Connection connection;
  

	public static void main(String[] args) {
		try {
			connection = new Connection(new URL("http://localhost:9363"));
			Session.loginWithPassword(connection, "any", "any");

			
			//---------SR---------
			/*Set<Pool> pools = Pool.getAll(connection);
	        Pool pool = (pools.toArray(new Pool[0]))[0];
	        SR sr = pool.getDefaultSR(connection);*/
			
	        //------VM--------
			/*VM.Record vmRecord = new VM.Record();
			
			vmRecord.nameLabel = "xen2.example.com";
			vmRecord.VCPUsAtStartup = new Long(1);
			vmRecord.VCPUsMax = new Long(1);
			vmRecord.actionsAfterShutdown = OnNormalExit.DESTROY;			
			vmRecord.actionsAfterReboot = OnNormalExit.RESTART;
			vmRecord.actionsAfterCrash = OnCrashBehaviour.RESTART;
			vmRecord.powerState = VmPowerState.HALTED; 
			vmRecord.memoryStaticMax = new Long(128*1024*1024);
			vmRecord.memoryStaticMin = new Long(0);
			vmRecord.memoryDynamicMax = new Long(128*1024*1024);
			vmRecord.PVKernel = "/boot/vmlinuz-2.6.24-27-xen";
			vmRecord.PVRamdisk = "/boot/initrd.img-2.6.24-27-xen";
			
			VM newVM = VM.create(connection, vmRecord);
			
			//--------VIF-------
	        VIF.Record vifRecord = new VIF.Record();
	        
	        Set<Network> networks = Network.getAll(connection);
	        Network network = null;
	        for (Network n : networks){
	        	network = n;
	        	break;
	        }
	        
	        //Network.Record nr = network.getRecord(connection);
	        //System.out.println(nr);

	        vifRecord.VM = newVM;
	        vifRecord.network = network;
	        vifRecord.device = "0";
	        vifRecord.MTU = 1500L;

	        VIF.create(connection, vifRecord);
	        
	        //--------VBD---------
			*/
			/*VBD.Record vbdRecord1 = new VBD.Record();
			vbdRecord1.bootable = true;
			vbdRecord1.device = "/dev/xvda1";
			vbdRecord1.mode = VbdMode.RO;
			vbdRecord1.type = VbdType.DISK;
			
			
			VDI.Record vdiRecord1 = new VDI.Record();
			vdiRecord1.location = "tap:aio:/home/xen/domains/xen1.example.com/swap.img,xvda1";
			vdiRecord1.readOnly = false;*/
			/*
			VDI.Record vdiRecord2 = new VDI.Record();
			vdiRecord2.location = "tap:aio:/home/xen/domains/xen1.example.com/disk.img,xvda2";
			vdiRecord2.readOnly = false;
			vdiRecord2.type = VdiType.SYSTEM;
			VDI vdi = VDI.create(connection, vdiRecord2);
			
			VBD.Record vbdRecord2 = new VBD.Record();
			vbdRecord2.VM = newVM;
			vbdRecord2.VDI = vdi;
			vbdRecord2.bootable = false;
			vbdRecord2.device = "/dev/xvda2";
			vbdRecord2.mode = VbdMode.RO;
			vbdRecord2.type = VbdType.DISK;
			VBD vbd = VBD.create(connection, vbdRecord2);
					
			newVM.provision(connection);
	        */
			Set<VM> allVMs = VM.getAll(connection);
			for(VM vm : allVMs){
				VM.Record tmpVmRecord = vm.getRecord(connection);
				
				if(tmpVmRecord.isControlDomain)
					System.out.println("Dom0: " + tmpVmRecord.uuid);
				else{
					System.out.println("DomU: " + tmpVmRecord.uuid);
					
					System.out.println(" <-------- VM Record: \n");
					System.out.println(tmpVmRecord.toString());

					System.out.println("<-------- residentOn: \n");
					System.out.println(tmpVmRecord.residentOn.getRecord(connection).toString());
					
					System.out.println("<-------- PIFs: \n");
					System.out.println(((PIF)tmpVmRecord.residentOn.getRecord(connection).PIFs.toArray()[0]).getRecord(connection).toString());					
					
					System.out.println("<-------- consoles: \n");
					System.out.println(((Console)tmpVmRecord.consoles.toArray()[0]).getRecord(connection).toString());
					
					System.out.println("<-------- VIFs: \n");
					System.out.println(((VIF)tmpVmRecord.VIFs.toArray()[0]).getRecord(connection).toString());					
					
					System.out.println("<-------- VBDs: \n");
					System.out.println(((VBD)tmpVmRecord.VBDs.toArray()[0]).getRecord(connection).toString());
					System.out.println(((VBD)tmpVmRecord.VBDs.toArray()[1]).getRecord(connection).toString());
					
					//vm.start(connection, false, false);
				}
			}
			
	        Session.logout(connection);
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println(e.getMessage());
		}
	}
}
